package com.tuozixuan;

import java.io.File;

import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.errors.TransportException;
import org.eclipse.jgit.transport.CredentialsProvider;
import org.eclipse.jgit.transport.RefSpec;
import org.eclipse.jgit.transport.RemoteSession;
import org.eclipse.jgit.transport.SshSessionFactory;
import org.eclipse.jgit.transport.SshTransport;
import org.eclipse.jgit.transport.URIish;
import org.eclipse.jgit.util.FS;

public class GitSshCredentialsDemo {

	public static void main(String[] args) throws Exception {
		SshSessionFactory sshSessionFactory = new SshSessionFactory() {
//		    @Override
//		    protected void configure(OpenSshConfig.Host host, Session session) {
//		        session.setConfig("StrictHostKeyChecking", "no");
//		    }
//
//		    @Override
//		    protected JSch createDefaultJSch(FS fs) throws JSchException {
//		        JSch sch = super.createDefaultJSch(fs);
//		        //keyPath 私钥文件 path
//		        String keyPath = ""; // TODO
//		        sch.addIdentity(keyPath);
//		        return sch;
//		    }

			@Override
			public RemoteSession getSession(URIish arg0, CredentialsProvider arg1, FS arg2, int arg3)
					throws TransportException {
				// TODO Auto-generated method stub
				return null;
			}

			@Override
			public String getType() {
				// TODO Auto-generated method stub
				return null;
			}
		};
		// 打开本地仓库
		Git git = Git.open(new File("/Users/zhimaxingzhe/test"));
		// 执行远程拉取合并
		git.pull().setTransportConfigCallback(
		    transport -> {
		        SshTransport sshTransport = ( SshTransport )transport;
		        sshTransport.setSshSessionFactory( sshSessionFactory );
		    }).call();
		// git push
		String branchName = "";
		git.push().setRemote("origin").setTransportConfigCallback(transport -> {
		  SshTransport sshTransport = ( SshTransport )transport;
		  sshTransport.setSshSessionFactory( sshSessionFactory );
		  }).setRefSpecs(new RefSpec(branchName)).call();


	}

}
